<template>
	<view class="agreement">
		<custom-nav v-bind="navConfig" :navStyle="{marginBottom: 0}" />
		<view v-if="!content" style="padding: 20px 0;">
			<u-loading-icon></u-loading-icon>
		</view>
		<!-- <u-parse v-else :content="content" style="padding: 10px 15px; background-color: #ffffff; line-height: 32px;" /> -->
		<view v-else v-html="content" style="padding: 40rpx; line-height: 48rpx;"></view>
	</view>
</template>

<script>
	import navConfig from './config/navConfig.js'
	import richTextConfig from './config/richTextConfig.js'

	// 引入数据库相关
	const db = uniCloud.database();
	const dbCollectionName = 'code-agreement';

	export default {
		data() {
			return {
				navConfig,
				richTextConfig,
				content: ''
			};
		},
		onLoad({
			opt
		}) {
			const {
				type
			} = JSON.parse(opt)
			this.initNavigation(type)
			this.getRichText(type)
		},
		methods: {
			initNavigation(type) {
				switch (type) {
					case 'PURCHASE_AGREEMENT_FB':
						this.navConfig.title = '购买协议'
						uni.setNavigationBarTitle({
							title: '购买协议'
						});
						break;
					case 'USER_AGREEMENT_FB':
						this.navConfig.title = '用户协议'
						uni.setNavigationBarTitle({
							title: '用户协议'
						});
						break;
					case 'PRIVACY_POLICY_FB':
						this.navConfig.title = '隐私政策'
						uni.setNavigationBarTitle({
							title: '隐私政策'
						});
						break;
					case 'SDK_AGREEMENT_FB':
						this.navConfig.title = '第三方SDK信息'
						uni.setNavigationBarTitle({
							title: '第三方SDK信息'
						});
						break;
				}
			},
			// 获取富文本
			getRichText(type) {
				// this.content = richTextConfig[type]
				// return
				// 显示加载中
				uni.showLoading({
					mask: true
				})
				// 从数据库中获取协议数据
				db.collection(dbCollectionName).where({
					agreement_key: type
				}).get().then((res) => {
					const data = res.result.data[0]
					if (data) {
						// 将获取到的数据赋值给formData
						this.content = data.content_html
						// this.navConfig.title = data.agreement_name
						// uni.setNavigationBarTitle({
						// 	title: data.agreement_name
						// });
					}
				}).catch((err) => {
					// 显示错误提示
					uni.showModal({
						content: err.message || '请求服务失败',
						showCancel: false
					})
				}).finally(() => {
					// 隐藏加载中
					uni.hideLoading()
				})
			}
		}
	}
</script>
<style lang="scss" scoped>

</style>